package com.jt.controller;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller //执行视图解析器的代码
//@RestController //一般适用于ajax	不走视图解析器,并且返回JSON数据
public class UserController {
	/**
	 * 需求1: 请求路径localhost/findAll 跳转到userList.jsp页面中
	 */

	@Autowired
	private UserMapper userMapper;

	@RequestMapping("findAll")
	public String findAll(Model model) { //model是springmvc 中提供操作request对象的API
		List<User> users = userMapper.selectList(null);
		model.addAttribute("userList", users);
		return "userList";
	}

	@RequestMapping("ajax")
	public String findAll() {
		return "userAjax";
	}

	//实现ajax业务调用,返回页面列表形式.
	@RequestMapping("findAjax")
	@ResponseBody
	public List<User> findAjax() {
		System.out.println("=========");
		return userMapper.selectList(null);
	}

	@RequestMapping("findAjax2")
	@ResponseBody //直接返回原字符串
	public String findAjax2(Integer id, String name) {
		System.out.println("用户新增成功");
		return "业务调用成功";
	}
}
